<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<link href="<s:url value='/ui/autocomplete/css/jquery.autocomplete.css'/> " rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<s:url value='/ui/autocomplete/jquery.autocomplete.js' />"></script>
<script type="text/javascript">
var userSelectTree = ctx + '/sm/User!findUserTree.action';
var callAll = false;
$(function(){
	$('#target_user_tree').tree({
		url: userSelectTree,
        fit: true,
        idField: 'id',
        treeField: 'text',
        onBeforeLoad : function(row, param) {
			if (row) {
				userSelectTree = ctx + '/sm/User!findUserTree.action?id='+row.id;
			}
		},
		onDblClick : function(node){
			if ($('#target_user_tree').tree("isLeaf", node.target)) {
				addI(node);
			}
		},onClick : function(node) {
			callAll = false;
		},onExpand : function(node) {
			if (callAll) {
					var nodeSelect = $('#target_user_tree').tree("getSelected");
					if (nodeSelect == null)
										return;
					if (node.id == nodeSelect.id|| isParent(node, nodeSelect)) {
							addItemNodes(node);
					} else {
						callAll = false;
					}
			}
		},onLoadSuccess: function(){
			$('#selectloadMsg').hide();
		}
	});
	
	
	
	$('#selectClosed','#selectUserOpenWindows').bind('click',function(){
		showHideFun('targetPopup','close');
	});
	
	$('#selectConform','#selectUserOpenWindows').bind('click',function(){
		var more = $('#operationOne').val();
		//获取选择的操作对象列表
		var opts = document.getElementById("selectedTargets").options;
		if (opts.length == 0) {
            $.messager.show({
                msg : "请选择人员!"
            });
            return false;
        }
		if('1'==more){
			if (opts.length != 1) {
	            $.messager.show({
	                msg : "请只选择一个人员!"
	            });
	            return false;
	        }
		}
		var rows = [];
		for ( var i = 0; i < opts.length; i++) {
			var row = {};
			row.id = opts[i].value;
			row.text = opts[i].text;
			row.mobile = $(opts[i]).attr('mobile');
			row.email = $(opts[i]).attr('email');
			rows.push(row);
		}
		CQOA.targetPopup(rows,$('#OperationTypes').val());
		showHideFun('targetPopup','close');
	});
	
	
	//本单位搜索
    $("#autoUserName").autocomplete(ctx + '/sm/User!findByName.action', {
        minChars: 1,
        width: 220,
        cacheLength: 1,
        matchContains: true,
        autoFill: false,
        matchSubset: false,
        multiple: true,
        postType: 'choose',
        onSelected: function(data){
        	var attr={
        			email:data.email,
        			mobile:data.mobile
        	};
        	var node={
        			id:'u'+data.sid,
        			text:data.cname,
        			attributes:attr
        	};
        	addI(node);
        },
        parse: function(data){//解析JSON数据
        	var rows = [];
            for (var i = 0; i < data.length; i++) {
            	//如果有电子邮件地址的，则加入row
            		rows[rows.length] = {
                        data: data[i],
                        value: data[i].sid,
                        result: data[i].cname
                    };
            }
            return rows;
        },
        formatItem: function(row, i, max){
        	return row.cname;
        }
    });
	
	
	
});
/**
 * 设置数据
 */
function selectSetting(rows){
	for ( var i = 0; i < rows.length; i++) {
		addI(rows[i]);
	}
};


//添加本级节点
function addItem() {
	var node = $('#target_user_tree').tree("getSelected");//选择的节点
	if (node == null)
		return;//如果为NULL，则返回
	if ($('#target_user_tree').tree("isLeaf", node.target)) {
			addI(node);
	}
};
//添加
function addI(node) {
	if (existItem(node.id)) {
		return;//如果已经存在，则返回
	}
	var opt = "<option id='option" + node.id + "' value='" + node.id + "' email='"+node.attributes.email+"' mobile='"+node.attributes.mobile+"'>" + node.text + "</option>";
	$(opt).appendTo($("#selectedTargets"));
	
}
//是否是他的父节点
function isParent(node,select) {
	var thisNode = $('#target_user_tree').tree("getParent", node.target);
	if (thisNode.id == select.id) {
		return true;
	} else {
		var root = $('#target_user_tree').tree("getRoot");
		if (thisNode.id == root.id) {
			return false;
		} else {
			return isParent(thisNode, select);
		}
	}
};

//添加所有节点
function addAllItem() {
	var node = $('#target_user_tree').tree("getSelected");
	if (node == null)
		return;
	if ($('#target_user_tree').tree("isLeaf", node.target)) {
		addI(node);//添加本结点
	}
	callAll = true;
	$('#target_user_tree').tree("expandAll", node.target);
	addItemNodes(node);
}
function addItemNodes(node) {
	//添加子结点
	var nodes = $('#target_user_tree').tree("getChildren", node.target);
	for ( var i = 0; i < nodes.length; i++) {
			var child = nodes[i];
			//全单位的时候点击全选择递归查询
			if(callAll){
				$('#target_user_tree').tree("expandAll", child.target);
			}
			if ($('#target_user_tree').tree("isLeaf", child.target)) {
				addI(child);
			}
	}
}
//移除指定的
function removeItem() {
	var lst = document.getElementById("selectedTargets");
	var idx = lst.selectedIndex;
	if (idx >= 0)
		lst.remove(idx);
	if (idx < lst.options.length)
		lst.selectedIndex = idx;
	else if (lst.options.length > 0)
		lst.selectedIndex = lst.options.length - 1;
}
//清空
function clearItem() {
	document.getElementById("selectedTargets").options.length = 0;
}
//判断是否存在
function existItem(key) {
	var result = false;
	$("#selectedTargets option").each(function() {
		if ($(this).val() == key) {
			result = true;
			return result;
		}
	});
	return result;
}

</script>
<div id="selectUserOpenWindows" style="width: 100%; height: 400px;">
	<table style="height: 100%;width: 100%;">
		<tr style="height: 40px;text-align: center;">
			<td colspan="3">
				人员姓名查询:<s:textfield id="autoUserName" cssClass="easyui-validatebox" style="width:50%" ></s:textfield>
			</td>
		</tr>
		<tr>
			<td style="width: 45%;">
				<!-- 选择的类型  1 人员  2部门  3岗位 -->
				<s:hidden name="selectTypes" value="1" id="OperationTypes"></s:hidden>
				<!-- 选择是否唯一或人数限制  1  唯一一个 0 不限制-->
				<s:hidden  id="operationOne"></s:hidden>
				<div style="width: 100%; height: 350px; overflow: auto;border: 1px solid;">
							<ul id="target_user_tree" class="easyui-tree"></ul>
							<div id="selectloadMsg">数据加载中,请稍候...</div>
				</div>
			</td>
			<td style="width: 5%;">
					<p><input type="image" src="<s:url value='/images/icon/a_select.jpg'/>" style="border: 1px solid;" title="选择本项" onClick="addItem();"></p>
					<p><input type="image" src="<s:url value='/images/icon/all_select.jpg'/>"  style="border: 1px solid;" title="选择本项以下所有" onClick="addAllItem();"></p>
					<p><input type="image" src="<s:url value='/images/icon/un_select.jpg'/>" style="border: 1px solid;" title="取消选择" onClick="removeItem();"></p>
					<p><input type="image" src="<s:url value='/images/icon/unAll_select.jpg'/>" style="border: 1px solid;" title="取消所有" onClick="clearItem();"></p>
			</td>
			<td style="width: 45%;">
				<select id="selectedTargets" name="select" size="2" style="width: 100%; height: 350px;" >
				</select>
			</td>
		</tr>
	</table>
	<div  border="false" style="height: 40px; text-align: center; width: 100%;padding-top: 5px;">
				<a href="#" class="easyui-linkbutton" iconCls="icon-ok" id="selectConform">确定</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" id="selectClosed">取消</a>
	</div>
</div>
